export XILINX_PATH=/home/bibo/eda/Xilinx/
source setup.sh
#source versal/run_syn_hpu_1part_psi16.sh

OUTDIR=$PROJECT_DIR/versal/output_psi16
TOP_NAME=top_hpu
TOP_TOP=TOP_TOP_hpu
PEP_MSPLIT=PEP_MSPLIT_main2_subs2
PEP_BATCH=PEP_BATCH_bpbs9_tpbs27
TOP_PCMAX=TOP_PCMAX_pem2_glwe1_bsk16_ksk16
TOP_PC=TOP_PC_pem2_glwe1_bsk16_ksk16
APPLICATION=APPLI_msg2_carry2_pfail128_132b_tuniform_144a47
NTT_MOD=NTT_MOD_goldilocks
NTT_CORE_ARCH=NTT_CORE_ARCH_gf64
NTT_CORE_R=NTT_CORE_R_2
NTT_CORE_PSI=NTT_CORE_PSI_16
NTT_CORE_RDX_CUT=NTT_CORE_RDX_CUT_n5c5c1
NTT_CORE_DIV=NTT_CORE_DIV_1
BSK_SLOT=BSK_SLOT_8
BSK_CUT=BSK_CUT_16
KSK_SLOT=KSK_SLOT_8
KSK_CUT=KSK_CUT_16
KSLB=KSLB_x2y32z3
HPU_PART=HPU_PART_gf64
AXI_DATA_W=AXI_DATA_W_256
REGF_STRUCT=REGF_STRUCT_reg64_coef32_seq4

INTER_PART_PIPE=2

STEP=new
export SCRIPT_DIR=$PROJECT_DIR/versal/scripts
VIVADO_OPT="P:INTER_PART_PIPE=$INTER_PART_PIPE D:MEMORY_FILE_PATH=\"${PROJECT_DIR}/hw/\""
RTL_FLAGS="-F TOP_TOP $TOP_TOP -F PEP_MSPLIT $PEP_MSPLIT -F PEP_BATCH $PEP_BATCH -F TOP_PCMAX $TOP_PCMAX -F TOP_PC $TOP_PC -F APPLICATION $APPLICATION -F NTT_MOD $NTT_MOD -F NTT_CORE_ARCH $NTT_CORE_ARCH -F NTT_CORE_R $NTT_CORE_R -F NTT_CORE_PSI $NTT_CORE_PSI -F NTT_CORE_RDX_CUT $NTT_CORE_RDX_CUT -F NTT_CORE_DIV $NTT_CORE_DIV -F BSK_SLOT $BSK_SLOT -F BSK_CUT $BSK_CUT -F KSK_SLOT $KSK_SLOT -F KSK_CUT $KSK_CUT -F KSLB $KSLB -F HPU_PART $HPU_PART -F AXI_DATA_W $AXI_DATA_W -F REGF_STRUCT $REGF_STRUCT"
export DESIGN_NAME=$TOP_NAME

# Remove previously generated files
rm -rf $OUTDIR/$TOP_NAME

# Treat unset variables as an error and exit immediately
set -euxo pipefail
# Generate ./versal/output_psi16/rtl/inc/top_defines_inc.sv, which only defines MEMORY_FILE_PATH
python3 $PROJECT_DIR/versal/scripts/declare_define/declare_define.py -l "$VIVADO_OPT" -d $OUTDIR
# Use run_edalize to generate file list in tcl_dict format
$PROJECT_DIR/hw/scripts/edalize/run_edalize.py -d $OUTDIR -t tcl_dict -m $TOP_NAME $RTL_FLAGS --tcl-dict-out $OUTDIR/top_hpu_edalize.tcl || exit 1
# Run Vivado with build_all.tcl
vivado -log $OUTDIR/vivado_integration_$STEP.log -nojournal -mode batch -source $SCRIPT_DIR/build_all.tcl -tclargs $OUTDIR $STEP "$VIVADO_OPT"
